package vip.xiaonuo.modular.xyuser.api;


import cn.hutool.extra.emoji.EmojiUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.core.annotion.version.ApiVersionSwagger;
import vip.xiaonuo.core.context.login.LoginContextHolder;
import vip.xiaonuo.modular.xyuser.entity.XyUser;
import vip.xiaonuo.modular.xyuser.form.WechatForm;
import vip.xiaonuo.modular.xyuser.form.WechatInfoForm;
import vip.xiaonuo.modular.xyuser.service.XyUserService;
import vip.xiaonuo.modular.xyuser.vo.UserCodeVo;
import vip.xiaonuo.sys.core.msg.Result;

import static vip.xiaonuo.sys.core.msg.ResultUtils.success;

@RestController
@Api(tags = "用户相关apis")
@ApiVersionSwagger
@RequestMapping("/user")
public class ApiUserController {

    @Autowired
    private XyUserService xyUserService;




    /**
     * 预登陆接口
     */
    @GetMapping("/readrLogin/{code}")
    @ApiOperation(value = "根据code 返回 小程序端所需内容", notes = "此接口不进行注册操作!但是返回token 操作,进行无感")
    public Result<UserCodeVo> readrLogin(@PathVariable String code) {
        return xyUserService.loginByCode(code);
    }

    /**
     * <pre>
     * 获取用户信息接口
     * </pre>
     */
    @PostMapping("/logon")
    @ApiModelProperty(value = "根据 login 接口返回的信息,二次拉取用户详细信息", notes = "登录接口,返回token")
    public Result<String> logon(@RequestBody WechatForm wechatForm) {
        // 解密用户信息
        return success(xyUserService.login(wechatForm));
    }

    /**
     * 获取用户手机号接口
     */
    @GetMapping("/getPhoneNumber/{code}")
    @ApiOperation(value = "获取用户手机号接口", notes = "获取用户手机号接口")
    public Result getPhoneNumber(@PathVariable String code) {
        xyUserService.getPhoneNumber(code);
        return success();
    }


    /**
     * 获取用户手机号接口
     */
    @PostMapping("/getNewPhoneNumber")
    @ApiOperation(value = "获取用户手机号接口", notes = "获取用户手机号接口")
    public Result getNewPhoneNumber(@RequestBody WechatInfoForm wechatInfoForm) {
        xyUserService.getNewPhoneNumber(wechatInfoForm);
        return success();
    }
    /**
     * 查询用户个人信息
     * @return
     */
    @GetMapping("/findThisInfo")
    @ApiModelProperty("查看个人信息")
    public Result<XyUser> findThisInfo(){
        Long userId = LoginContextHolder.me().getSysLoginUser().getId();
        XyUser xyUser = xyUserService.getById(userId);
        xyUser.setNickName(EmojiUtil.toUnicode(xyUser.getNickName()));
        return success(xyUser);
    }

    /**
     *  完善手机号
     */
    @PostMapping("/bindPhoneAuth")
    @ApiOperation("完善手机号")
    public Result bindPhoneAuth(){
        //xyUserService.bindPhoneAuth();
        return success();
    }



}
